/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.staticinfo.controller;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jzwl.common.constant.AjaxStatusConstant;
import com.jzwl.common.constant.Constants;
import com.jzwl.common.page.PageObject;
import com.jzwl.common.upload.FileUploadUtils;
import com.jzwl.site.manager.staticinfo.pojo.SiteStaticinfo;
import com.jzwl.site.manager.staticinfo.service.SiteStaticinfoService;
import com.jzwl.system.base.controller.BaseController;

/**
 * SiteStaticinfo
 * SiteStaticinfo
 * siteStaticinfo
 * <p>Title:SiteStaticinfoController </p>
 * 	<p>Description:SiteStaticinfo </p>
 * 	<p>Company: </p> 
 * 	@author aotu-code 
 */
@Controller
@RequestMapping("/siteStaticinfo")
public class SiteStaticinfoController extends BaseController {

	@Autowired
	private SiteStaticinfoService siteStaticinfoService;

	@Autowired
	private Constants constants;

	/**
	 * 跳转到添加SiteStaticinfo的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toAddSiteStaticinfo")
	public ModelAndView toAddsiteStaticinfo(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);
		mov.addObject("type", paramsMap.get("type"));
		mov.setViewName("/manager/staticinfo/add");
		return mov;
	}

	/**
	 * 添加SiteStaticinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addSiteStaticinfo")
	public ModelAndView addsiteStaticinfo(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {
			boolean flag = siteStaticinfoService.addSiteStaticinfo(paramsMap);
			if (!flag) {
				mov.addObject("msg", "添加SiteStaticinfo失败!");
				mov.setViewName("/error");

			} else {

				mov.setViewName("redirect:/siteStaticinfo/siteStaticinfoList.html?type=" + paramsMap.get("type"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "添加SiteStaticinfo失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * siteStaticinfolist
	 */
	@RequestMapping(value = "/siteStaticinfoList")
	public ModelAndView siteStaticinfoList(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		PageObject po = new PageObject();

		try {
			paramsMap.put("isDelete", 0);
			po = siteStaticinfoService.querySiteStaticinfoList(paramsMap);

			mov.addObject("list", po.getDatasource());
			mov.addObject("totalProperty", po.getTotalCount());
			mov.addObject("page", po);
			mov.addObject("type", paramsMap.get("type"));

			//图片服务器路径
			mov.addObject("projectPath", constants.getSftz_online_path());

			mov.addObject("status", paramsMap.get("status"));

			mov.setViewName("/manager/staticinfo/list");
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "siteStaticinfo查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 跳转到修改siteStaticinfo页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toUpdateSiteStaticinfo")
	public ModelAndView toUpdateSiteStaticinfo(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			SiteStaticinfo siteStaticinfo = siteStaticinfoService.getById(paramsMap);

			//图片服务器路径
			mov.addObject("projectPath", constants.getSftz_online_path());
			mov.addObject("obj", siteStaticinfo);
			mov.setViewName("/manager/staticinfo/edit");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteStaticinfo查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 修改siteStaticinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateSiteStaticinfo")
	public ModelAndView updateSiteStaticinfo(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteStaticinfoService.updateSiteStaticinfo(paramsMap);

			if (!flag) {
				mov.addObject("msg", "修改失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteStaticinfo/siteStaticinfoList.html?type=" + paramsMap.get("type"));
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "修改失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	@RequestMapping(value = "/pub")
	public @ResponseBody Map<String, Object> updateStatus(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
		createParameterMap(request);
		try {
			boolean flag = siteStaticinfoService.updateStatus(paramsMap);
			if (!flag) {
				resultMap.put("msg", "修改失败!");
			} else {
				resultMap.put("error", "异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("error", "异常");
		}
		return resultMap;
	}

	/**
	 * 逻辑删除siteStaticinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delSiteStaticinfo")
	public ModelAndView delSiteStaticinfo(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteStaticinfoService.delSiteStaticinfo(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteStaticinfo/siteStaticinfoList.html?type=" + paramsMap.get("type"));
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	/**
	 * 物理删除siteStaticinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deleteSiteStaticinfo")
	public ModelAndView deleteSiteStaticinfo(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteStaticinfoService.deleteSiteStaticinfo(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteStaticinfo/siteStaticinfoList.html?type=" + paramsMap.get("type"));
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	@RequestMapping(value = "uploadFirstImage", method = RequestMethod.POST)
	public @ResponseBody Map uploadFirlstImage(@RequestParam("img") MultipartFile img, HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		if (img.isEmpty()) {
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_FAILURE);
			map.put("msg", "图片不能为空！");
			return map;
		}
		try {
			String filename = URLEncoder.encode(img.getOriginalFilename(), "utf-8");
			long datelong = System.currentTimeMillis();
			String imgPath = constants.getSftz_online_path() + datelong + "_" + filename;
			String targetPath = constants.getSftz_online_path() + imgPath;
			FileUploadUtils.uploadToStaticResourceServerByJersey(img, targetPath);

			map.put("picUrl", imgPath);// 相对路径，存数据库的。
			map.put("targetPath", targetPath);// 绝对路径，显示
			map.put("filename", filename);
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
			return map;
		} catch (Exception e) {
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_FAILURE);
			map.put("msg", "上传失败！" + e.getMessage());
			e.printStackTrace();
			return map;
		}
	}

}
